#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb
import redis
import urllib
import mysql

refreshIf = "http://p2p.tvmore.com.cn/refreshGeneral?groupID=%s"
general_fmt = "moretv_p2p_group_general_%d"
rClient = redis.Redis(
    host = "10.10.218.112",
    port = 6379,
    db = 0
)

def refresh() :
    keyList = mysql.queryRoute()
    for row in keyList :
        routeId = row[0]
        routeInfo = rClient.hgetall(general_fmt % routeId)
        # struct of general_key :
        # {
        #     $groupId1: {
        #         code : $channel,
        #         peersStat : {
        #             1 : $num1,
        #             2 : $num2,
        #             3 : $num3,
        #             4 : $num4,
        #             5 : $num5
        #         }
        #     },
        #     $groupId2: {
        #         ...
        #     },
        #     ...
        # }
        for groupId in routeInfo :
            url = refreshIf % groupId
            f = urllib.urlopen(url)
            s = f.read()
            print s

def main() :
    refresh()

if __name__ == "__main__" :
    main()
